package com.cpi.cornpp.distributionfutureversion.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.distributionfutureversion.exception.DistributionFutureVersionException;
import com.cpi.cornpp.distributionfutureversion.service.DistributionFutureVersionService;
import com.cs.common.utils.NumberUtil;
/**
 * 
 * Distribucion de Futuros en Version  - Action
 * 
 * @author Edgar Joao
 * @see MappingDispatchAction
 *
 */
public class DistributionFutureVersionAction extends MappingDispatchAction {
	/** Identifier default workflow*/
	private final static String DEFAULT = "default";//$NON-NLS-1$
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Success forward */
	private final static String SUCCESS_FORWARD = "success";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_ADD_LABEL = "distribution.add.success";//$NON-NLS-1$
	/** Success delete key */
	private static final String SUCCESS_DELETE_LABEL = "distribution.delete.success";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Version ID in Session*/
	private static final String VERSION_ID = "versionId";
	/** futurePeriod ID in Session*/
	private static final String FUTURE_PERIOD_ID = "futurePeriodId";
	/** Distribution future version detail view in request*/
	private static final String DISTRIBUTION_FUTURE_VERSION_DETAIL_VIEWS = "distributionFutureVersionDetailViews";
	/** Distribution future version list in request*/
	private String DISTRIBUTION_FUTURE_VERSION_LIST = "distributionFutureVersionList";
	/**
	 * Muestra el listado de Distribucion de Futuros en Version
	 * 
	 * @author Edgar Joao
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward distributionFutureVersionList(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)throws DistributionFutureVersionException {
		HttpSession httpSession = null;
		int versionId = 0;
		DistributionFutureVersionService distributionFutureVersionService = null;
		List distributionFutureVersionList = null;
		try{
			httpSession = request.getSession();
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID));
			distributionFutureVersionList = new ArrayList(0);
			distributionFutureVersionService = new DistributionFutureVersionService();		
			distributionFutureVersionList = distributionFutureVersionService.getFuturesByVersionId(new Integer(versionId));
			request.setAttribute(DISTRIBUTION_FUTURE_VERSION_LIST , distributionFutureVersionList);					
		}catch (DistributionFutureVersionException distributionFutureVersionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, distributionFutureVersionException);
			throw distributionFutureVersionException;
		}
		catch (Exception e){
			DistributionFutureVersionException distributionFutureVersionException=null;
			e.printStackTrace(System.out);
			distributionFutureVersionException = new DistributionFutureVersionException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, distributionFutureVersionException);
			throw distributionFutureVersionException;
		}	
		
		return mapping.findForward(DEFAULT);
	}
	/**
	 * Muestra el Detalle de Distribucion de Futuros en Version
	 * 
	 * @author Edgar Joao
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward showDistributionFutureVersionDetail(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws DistributionFutureVersionException {
		DistributionFutureVersionService distributionFutureVersionService = null;
		List distributionFutureVersionDetailViews = null;
		String futurePeriodId = null;
		HttpSession httpSession = null;
		int versionId = 0;
		try{		
			httpSession = request.getSession();
			futurePeriodId = request.getParameter(FUTURE_PERIOD_ID);
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID));
			distributionFutureVersionDetailViews = new ArrayList(0);
			distributionFutureVersionService = new DistributionFutureVersionService();		
			distributionFutureVersionDetailViews = distributionFutureVersionService.getDistributionFutureVersionDetailsByFuturePeriodId((new Integer(NumberUtil.parseInt(futurePeriodId))), new Integer(versionId));
			request.setAttribute(DISTRIBUTION_FUTURE_VERSION_DETAIL_VIEWS , distributionFutureVersionDetailViews);					
		}catch (DistributionFutureVersionException distributionFutureVersionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, distributionFutureVersionException);
			throw distributionFutureVersionException;
		}
		catch (Exception e){
			DistributionFutureVersionException distributionFutureVersionException=null;
			e.printStackTrace(System.out);
			distributionFutureVersionException = new DistributionFutureVersionException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, distributionFutureVersionException);
			throw distributionFutureVersionException;
		}	
		
		
		
		
		return mapping.findForward(DEFAULT);
	}
	/**
	 * Metodo que agrega una distribucion
	 * 
	 * @author Edgar Joao
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addDistribution(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws DistributionFutureVersionException {
		HttpSession session = null;
		DistributionFutureVersionService service = null;
		MessageResources messageResources = null;
		String message = null;
		String versionId = null;
		
		session = request.getSession(true);
		messageResources = getResources(request);
		try{
			service = new DistributionFutureVersionService();
			versionId = (String) session.getAttribute(VERSION_ID);
			service.addDistribution(Integer.parseInt(versionId));			
			message = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);			
		} catch(DistributionFutureVersionException distributionFutureVersionException){			
			session.setAttribute(CUSTOM_EXCEPTION, distributionFutureVersionException);
			throw distributionFutureVersionException;
		} catch(Exception e){
			DistributionFutureVersionException distributionFutureVersionException=null;
			e.printStackTrace(System.out);
			distributionFutureVersionException= new DistributionFutureVersionException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_INSERT);
			session.setAttribute(CUSTOM_EXCEPTION, distributionFutureVersionException);
			throw distributionFutureVersionException;
		}		
		session.setAttribute(MESSAGE, message);
		return mapping.findForward(SUCCESS_FORWARD);
	}
	/**
	 * Metodo que elimina una distribucion
	 * 
	 * @author Edgar Joao
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delDistribution(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws DistributionFutureVersionException {		
		HttpSession session = null;
		DistributionFutureVersionService service = null;
		MessageResources messageResources = null;
		String message = null;
		String versionId = null;
		
		messageResources = getResources(request);
		session = request.getSession(true);
		try{
			service = new DistributionFutureVersionService();
			versionId = (String) session.getAttribute(VERSION_ID);
			service.delDistribution(Integer.parseInt(versionId));
			message = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELETE_LABEL);			
		} catch(DistributionFutureVersionException  distributionFutureVersionException){
			session.setAttribute(CUSTOM_EXCEPTION,  distributionFutureVersionException);
			throw distributionFutureVersionException;
		} catch(Exception e){
			DistributionFutureVersionException distributionFutureVersionException=null;
			e.printStackTrace(System.out);
			distributionFutureVersionException= new DistributionFutureVersionException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_DELETE);
			session.setAttribute(CUSTOM_EXCEPTION, distributionFutureVersionException);
			throw distributionFutureVersionException;
		}
		session.setAttribute(MESSAGE, message);
		return mapping.findForward(SUCCESS_FORWARD);
	}
}
